package com.bonc.excel.controller.admin;

import com.bonc.excel.entity.ColDto;
import com.bonc.excel.service.CateCnfInfoService;
import com.bonc.excel.service.ColService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;


/**
 * @Description:
 * @Date: Created in 14:56 2022/6/2
 * STAR

 */
@Controller
@RequestMapping("/admin")
public class ColController {

    @Autowired
    private ColService colService;
    @Autowired
    private CateCnfInfoService cateCnfInfoService;

    //    分页查询分类列表
    @GetMapping("/cols")
    public String list(Model model, @RequestParam(required = false,value = "categroyCode") String categroyCode,
                       @RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum){
        //按照排序字段 倒序 排序
        //String orderBy = "col_level asc";
        String orderBy = "categroy_code asc";
        PageHelper.startPage(pageNum,10,orderBy);
        List<ColDto> list = colService.lambdaQuery()
                .like(StringUtils.isNotBlank(categroyCode) , ColDto::getCategroyCode,categroyCode)
                .list();
        //List<Type> list = typeService.getAllType();
        PageInfo<ColDto> pageInfo = new PageInfo<ColDto>(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("categroyCode",categroyCode);
        return "admin/cols";
    }

    //    返回新增分类页面
    @GetMapping("/cols/input")
    public String input(Model model){
        model.addAttribute("type", new ColDto());
        model.addAttribute("categroyCode",cateCnfInfoService.lambdaQuery().list());
        return "admin/cols-input";
    }

    //  新增分类
    @PostMapping("/cols")
    public String post( ColDto type, RedirectAttributes attributes) {
      //  Type type1 = typeService.getTypeByName(type.getName());
      /*  if (type1 != null) {
            attributes.addFlashAttribute("message", "不能添加重复的分类");
            return "redirect:/admin/types/input";
        }*/
        boolean save = colService.save(type);
        //int t = typeService.saveType(type);
        if (!save) {
            attributes.addFlashAttribute("message", "新增失败");
        } else {
            attributes.addFlashAttribute("message", "新增成功");
        }
        return "redirect:/admin/cols";
    }

    //    跳转修改分类页面
    @GetMapping("/cols/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {

        model.addAttribute("type",   colService.getById(id));
        return "admin/cols-input";
    }

    //    编辑修改分类
    @PostMapping("/cols/{id}")
    public String editPost( ColDto type, RedirectAttributes attributes) {
        ColDto type1 = colService.getById(type.getId());
       // Type type1 = typeService.getTypeByName(type.getName());
      /*  if (type1 != null) {
            attributes.addFlashAttribute("message", "不能添加重复的分类");
            return "redirect:/admin/types/input";
        }*/
        type.setCategroyCode(type1.getCategroyCode());
        boolean b = colService.updateById(type);
       // int t = typeService.updateType(type);
        if (!b ) {
            attributes.addFlashAttribute("message", "编辑失败");
        } else {
            attributes.addFlashAttribute("message", "编辑成功");
        }
        return "redirect:/admin/rows";
    }

    //    删除分类
    @GetMapping("/cols/{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes) {
        colService.removeById(id);
        //typeService.deleteType(id);
        attributes.addFlashAttribute("message", "删除成功");
        return "redirect:/admin/cols";
    }

}